package com.lb.shopguide.config;

import android.content.Context;
import android.content.res.AssetManager;
import android.graphics.Typeface;

import com.lb.shopguide.constants.AppConstant;


/**
 * Created by zsd on 2016/12/13.
 * 保存用户的一些信息
 */

public class UserConfigManager extends BaseSpConfig {
    /**
     * 配置文件SharedPreferences名称
     */
    private final static String SP_FILE_NAME = "userConfig";
    private final static int TENANTID = 2;
    /**
     * 配置文件SharedPreferences名称
     */
    private static String mSpFileName;
    ///////////////////////////// 相关常量、变量 //////////////////////////
    /**
     * 用户配置单例对象
     */
    private static UserConfigManager mUserConfigManager;
    /**
     * 用户ID
     */
    private String usercode;
    /**
     * 用户token
     */
    private String userToken;
    /**
     * im token
     */
    private String imToken;
    /**
     * accId
     */
    private String accId;
    /**
     * 商店code
     */
    private String storeCode;
    /**
     * 商店logo url
     */
    private String storeLogoUrl;
    /**
     * 手机号
     */
    private String userName;
    /**
     * 用户名
     */
    private String mobileNum;
    /**
     * 用户头像url
     */
    private String userImageUrl;
    private String districtInfo;
    /**
     * 第一次
     */
    private boolean isFirstTime;
    /**
     *
     */
    private boolean showUpdate;
    /**
     *
     */
    private String updateVersion;
    /**
     * 秘籍数据
     */
    private String mijiInfo;
    /**
     * 字体
     */
    private Typeface typeface;

    private boolean isRecOpen = false;

    private boolean firstPooDialogGuide = false;

    private boolean secondPooDialogGuide = false;

    private int terminalType;

    private UserConfigManager(Context ctx) {
        super.initBaseConfig(ctx);
        //同步数据到内存中
        mSpFileName = SP_FILE_NAME + ".preferences";
        importPref2Mem();
        AssetManager assets = ctx.getAssets();
        typeface = Typeface.createFromAsset(assets, "fonts/FZLanTingHeiS-EL-GB.TTF");
    }

    private void importPref2Mem() {
        usercode = getString(mSpFileName, AppConstant.PRE_KEY_USERID, "");
        userToken = getString(mSpFileName, AppConstant.PRE_KEY_USERTOKEN, "");
        userName = getString(mSpFileName, AppConstant.PRE_KEY_USERNAME, "");
        isFirstTime = getBoolean(mSpFileName, AppConstant.PRE_KEY_APP_INIT, true);
        userImageUrl = getString(mSpFileName, AppConstant.PRE_KEY_USER_URL, "");
        storeCode = getString(mSpFileName, AppConstant.PRE_KEY_STORE_CODE, "");
        imToken = getString(mSpFileName, AppConstant.PRE_KEY_IMTOKEN, "");
        accId = getString(mSpFileName, AppConstant.PRE_KEY_ACCID, "");
        storeLogoUrl = getString(mSpFileName, AppConstant.PRE_KEY_STORE_URL, "");
        districtInfo = getString(mSpFileName, AppConstant.PRE_KEY_DISTRICT, "");
        terminalType = getInt(mSpFileName, AppConstant.PRE_KEY_TERMINAL_TYPE, -1);
    }

    /**
     * @param ctx
     * @return
     * @desc 单例获取UserConfig对象
     */
    public static UserConfigManager getInstance(Context ctx) {
        if (mUserConfigManager == null) {
            mUserConfigManager = new UserConfigManager(ctx);
        } else if (mUserConfigManager.mContext == null) {
            mUserConfigManager.mContext = ctx;
        }
        return mUserConfigManager;
    }

    /**
     * 判断是否登录
     *
     * @return
     */
    public String getUserId() {
        return getString(mSpFileName, AppConstant.PRE_KEY_USERID, "");
    }

    public void setUserId(String value) {
        setString(mSpFileName, AppConstant.PRE_KEY_USERID, (value == null ? "" : value));
    }

    public Integer getPreUserId() {
        return getInt(mSpFileName, AppConstant.PRE_KEY_PREUSERID, -1);
    }

    public void setPreUserId(Integer value) {
        setInt(mSpFileName, AppConstant.PRE_KEY_PREUSERID, (value == 0 ? 0 : value));
    }

    public String getUserToken() {
        return getString(mSpFileName, AppConstant.PRE_KEY_USERTOKEN, "");
    }


    public void setUserToken(String value) {
        setString(mSpFileName, AppConstant.PRE_KEY_USERTOKEN, (value == null ? "" : value));
    }

    public String getImToken() {
        return getString(mSpFileName, AppConstant.PRE_KEY_IMTOKEN, "");
    }


    public void setImToken(String value) {
        setString(mSpFileName, AppConstant.PRE_KEY_IMTOKEN, (value == null ? "" : value));
    }

    public String getAccId() {
        return getString(mSpFileName, AppConstant.PRE_KEY_ACCID, "");
    }


    public void setAccId(String value) {
        setString(mSpFileName, AppConstant.PRE_KEY_ACCID, (value == null ? "" : value));
    }

    public String getStoreCode() {
        return getString(mSpFileName, AppConstant.PRE_KEY_STORE_CODE, "");
    }

    public void setStoreCode(String storeCode) {
        setString(mSpFileName, AppConstant.PRE_KEY_STORE_CODE, (storeCode == null ? "" : storeCode));
    }

    public String getStoreLogoUrl() {
        return getString(mSpFileName, AppConstant.PRE_KEY_STORE_URL, "");
    }

    public void setStoreLogoUrl(String storeCode) {
        setString(mSpFileName, AppConstant.PRE_KEY_STORE_URL, (storeCode == null ? "" : storeCode));
    }

    public String getUserName() {
        return getString(mSpFileName, AppConstant.PRE_KEY_USERNAME, "");
    }

    public void setUserName(String value) {
        setString(mSpFileName, AppConstant.PRE_KEY_USERNAME, (value == null ? "" : value));
    }

    public String getRealname() {
        return getString(mSpFileName, AppConstant.PRE_KEY_REALNAME, "");
    }

    public void setRealname(String value) {
        setString(mSpFileName, AppConstant.PRE_KEY_REALNAME, (value == null ? "" : value));
    }

    public String getUserImageUrl() {
        return getString(mSpFileName, AppConstant.PRE_KEY_USER_URL, "");
    }

    public void setUserImageUrl(String userImageUrl) {
        setString(mSpFileName, AppConstant.PRE_KEY_USER_URL, (userImageUrl == null ? "" : userImageUrl));
    }

    public String getMobileNum() {
        return getString(mSpFileName, AppConstant.PRE_KEY_MOBILE_NUM, "");
    }

    public void setMobileNum(String mobileNum) {
        setString(mSpFileName, AppConstant.PRE_KEY_MOBILE_NUM, (mobileNum == null ? "" : mobileNum));
    }

    public String getUpdateVersion() {
        return getString(mSpFileName, AppConstant.PRE_KEY_UPDATE_VERSION, "");
    }

    public void setUpdateVersion(String updateVersion) {
        setString(mSpFileName, AppConstant.PRE_KEY_UPDATE_VERSION, (updateVersion == null ? "" : updateVersion));
    }

    public String getMijiInfo() {
        return getString(mSpFileName, AppConstant.PRE_KEY_MIJI_INFO, "");
    }

    public void setMijiInfo(String mijiInfo) {
        setString(mSpFileName, AppConstant.PRE_KEY_MIJI_INFO, (mijiInfo == null ? "" : mijiInfo));
    }


    public boolean isAppInitTag() {
        return getBoolean(mSpFileName, AppConstant.PRE_KEY_APP_INIT, true);
    }

    public void setAppInitTag(boolean appInitTag) {
        setBoolean(mSpFileName, AppConstant.PRE_KEY_APP_INIT, appInitTag);
    }


    public boolean isPushMessage() {
        return getBoolean(mSpFileName, AppConstant.PRE_KEY_PUSH_MESSAGE, true);
    }

    public void setIsPushMessage(boolean isPushMessage) {
        setBoolean(mSpFileName, AppConstant.PRE_KEY_PUSH_MESSAGE, isPushMessage);
    }

    public String getDistrictInfo() {
        return getString(mSpFileName, AppConstant.PRE_KEY_DISTRICT, "");
    }

    public void setDistrictInfo(String districtInfo) {
        setString(mSpFileName, AppConstant.PRE_KEY_DISTRICT, (districtInfo == null ? "" : districtInfo));
    }

    public int getTerminalType() {
        return getInt(mSpFileName, AppConstant.PRE_KEY_TERMINAL_TYPE, -1);
    }

    public void setTerminalType(int terminalType) {
        setInt(mSpFileName, AppConstant.PRE_KEY_TERMINAL_TYPE, terminalType);
    }

    public boolean isFirstTime() {
        return getBoolean(mSpFileName, AppConstant.PRE_KEY_IS_FIRST_IN, true);
    }

    public void setFirstTime(boolean firstTime) {
        setBoolean(mSpFileName, AppConstant.PRE_KEY_IS_FIRST_IN, isFirstTime);
    }

    public boolean isShowUpdate() {
        return getBoolean(mSpFileName, AppConstant.PRE_KEY_IS_SHOW_UPDATE, true);
    }

    public void setShowUpdate(boolean showUpdate) {
        setBoolean(mSpFileName, AppConstant.PRE_KEY_IS_SHOW_UPDATE, showUpdate);
    }


    public Typeface getTypeface() {
        return typeface;
    }

    public boolean isRecOpen() {
        return getBoolean(mSpFileName, AppConstant.PRE_KEY_REC_OPEN, false);
    }

    public void setRecOpen(boolean recOpen) {
        setBoolean(mSpFileName, AppConstant.PRE_KEY_REC_OPEN, recOpen);
    }

    public boolean isFirstPooDialogGuide() {
        return getBoolean(mSpFileName, AppConstant.PRE_KEY_FIRST_POP_DIALOG, false);
    }

    public void setFirstPooDialogGuide(boolean firstPooDialogGuide) {
        setBoolean(mSpFileName, AppConstant.PRE_KEY_FIRST_POP_DIALOG, firstPooDialogGuide);
    }

    public boolean isSecondPooDialogGuide() {
        return getBoolean(mSpFileName, AppConstant.PRE_KEY_SECOND_POP_DIALOG, false);
    }

    public void setSecondPooDialogGuide(boolean secondPooDialogGuide) {
        setBoolean(mSpFileName, AppConstant.PRE_KEY_SECOND_POP_DIALOG, secondPooDialogGuide);
    }
}
